﻿using System.Web.Mvc;
using Headspring.Training.Core.Services;
using Headspring.Training.Core.Services.Impl;
using Headspring.Training.Infrastructure;
using Headspring.Training.Infrastructure.Impl;
using Headspring.Training.UI.Controllers;
using StructureMap;

namespace Headspring.Training.DependencyResolution
{
	public class DependencyRegistrar
	{
		public void RegisterDependencies()
		{
			Register<IUserSession, HttpUserSession>();
			Register<IStateCommandVisitor, StateCommandVisitor>();
			Register<IWorkOrderBuilder, WorkOrderBuilder>();
			Register<IWorkOrderNumberGenerator, AlphanumericNumberGenerator>();
			Register<ICalendar, SystemCalendar>();
			Register<IWorkflowFacilitator, WorkflowFacilitator>();
			Register<IApplicationInformation, AssemblyInfoApplicationInformation>();
			Register<ICryptographyService, CryptographyService>();
			Register<IAuthenticationService, AuthenticationService>();

			Register<IWorkOrderRepository, WorkOrderRepository>();
			Register<IEmployeeRepository, EmployeeRepository>();

			Register<IEmailService, EmailService>();
			Register<INotifier, SmtpNotifier>();
			Register<ISessionBuilder, HybridSessionBuilder>();

			ControllerBuilder.Current.SetControllerFactory(new ControllerFactory());

			Register<LoginController>();
			Register<SearchController>();
			Register<WorkOrderController>();
		}

		private static void Register<I, C>() where C : I
		{
			StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;
			StructureMapConfiguration.BuildInstancesOf<I>().TheDefaultIsConcreteType<C>();
			Register<C>();
		}

		private static void Register<C>()
		{
			StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;
			StructureMapConfiguration.BuildInstancesOf<C>().
				TheDefaultIsConcreteType<C>();
		}
	}
}